<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.module.helper');

require_once (JPATH_SITE . DS . 'components' . DS . 'com_content' . DS . 'helpers' . DS . 'route.php');

class modArticleflowHelper {

    function getList(&$params) {
        global $mainframe;
        $frontpage = $params->get('frontpage');


        $catid = null;
        $sectionid = null;
        $select = $params->get('section_category', 1);
       
        if ($select == 1) {
            $sectionid = trim($params->get('seccat_id'));
        } else if ($select == 0) {
            $catid = trim($params->get('seccat_id'));
        }

        $count = (int) $params->get('count');
        $db = & JFactory::getDBO();
        $user = & JFactory::getUser();

        if ($catid || $sectionid) {

            $query = modArticleflowHelper::_buildQuery($catid, $sectionid, $count, $frontpage);
        }
        $db->setQuery($query);
        $rows = $db->loadObjectList();
        $i = 0;
        $data = array();

        foreach ($rows as $row) {
            $data[$i]->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->gslug, $row->sectionid));
            $data[$i]->text = htmlspecialchars($row->title);
            $data[$i]->introtext = $row->introtext;
            $data[$i]->fulltext = $row->fulltext;
            $data[$i]->created = $row->created;
            $i++;
        }
        return $data;
    }

    function _buildQuery($catid, $sectionid, $count, $frontpage) {

        $where = modArticleflowHelper::_buildContentWhere($catid, $sectionid, $frontpage);

        $query = 'SELECT d.*, ' .
                ' CONCAT_WS(\':\', d.id, d.alias) AS slug, ' .
                ' CONCAT_WS(\':\', c.id, c.alias) AS gslug ' .
                ' FROM #__content AS d' .
                ' LEFT JOIN #__categories AS c ON c.id = d.catid' .
                $where .
                ' LIMIT ' . $count

        ;

        return $query;
    }

    function _buildContentWhere($catid, $sectionid, $frontpage) {
        global $mainframe, $option;
        $db = & JFactory::getDBO();

        $where = array();

        if ($catid) {
            $ids = explode(',', $catid);
            JArrayHelper::toInteger($ids);
            $catCondition = '  (d.catid=' . implode(' OR d.catid=', $ids) . ')';
            $where[] = ($catid ? $catCondition : '');
        }
        if ($sectionid) {
            $ids = explode(',', $sectionid);
            JArrayHelper::toInteger($ids);
            $secCondition = '  (d.sectionid=' . implode(' OR d.sectionid=', $ids) . ')';
            $where[] = ($sectionid ? $secCondition : '');
        }
        if ($frontpage == 0) {
            $where[] = 'd.id IN (SELECT content_id FROM #__content_frontpage)';
        }
        $where = ( count($where) ? ' WHERE ' . implode(' AND ', $where) : '' );

        return $where;
    }

// Ham` lay' Image va` duong dan file Image
    function str_img_scr($html) {

        $image_regex_src_url = '/<img[^>]*' .
                'src=[\"|\'](.*)[\"|\']/Ui';
        preg_match_all($image_regex_src_url, $html, $out, PREG_PATTERN_ORDER);

        $img_tag_array = $out[0];

//        echo "<pre>";
//        print_r($img_tag_array);
//        echo "</pre>";
        // Regex for SRC Value
        $image_regex_src_url = '/<img[^>]*' .
                'src=[\"|\'](.*)[\"|\']/Ui';

        preg_match_all($image_regex_src_url, $html, $out, PREG_PATTERN_ORDER);

        $images_url_array = $out[1];

//        echo "<pre>";
//        print_r($images_url_array);
//        echo "</pre>";
        return $images_url_array;
    }

    // Fetch Page Function

    function FetchPage($path) {
        $file = fopen($path, "r");

        if (!$file) {
            exit("The was a connection error!");
        }

        $data = '';

        while (!feof($file)) {
// Extract the data from the file / url

            $data .= fgets($file, 1024);
        }
        return $data;
    }

}

?>
